package edu.hawaii.teams.halealohacli.command;

import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;

/**
 * Finds the current power consumption for sources in the Hale Aloha residences.
 * 
 * @author Branden Ogata
 * 
 */
public class CurrentPower implements Command {

  /**
   * Finds the current power consumption for the source or sources provided in details.
   * 
   * @param client A WattDepotClient to find the source in.
   * @param details String instances identifying the sources to examine.
   * @return data A String containing the power consumption for the sources. If multiple data exist,
   * places each on a new line.
   * @throws Exception Handles any problems that may occur.
   * 
   */
  @Override
  public String getData(WattDepotClient client, String... details) throws Exception {
    // Constant double used to convert from watt to kilowatt.
    final double KILO = 1000.0;
    StringBuilder buffer = new StringBuilder();

    // For each source provided
    for (String target : details) {
      try {
        buffer.append(client.getLatestPowerConsumed(target) / KILO + "\n");
      }
      catch (MiscClientException e) {
        buffer.append("Error: An error occurred while retrieving data.\n\n");
      }
      catch (ResourceNotFoundException e) {
        buffer.append("Error: The source " + target + " could not be found.\n\n");
      }
    }
    return buffer.toString();
  }
}